package com.fita.admin.dao;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.fita.entity.agent.*;
import com.fita.entity.principal.District;
import com.fita.entity.principal.Principal;

import java.util.List;



/**
 * 
 * @author sjh
 *
 */
@Dao("hibernate")
public interface AgentAuditDao {
	/**
	 * 
	 * @param companyName
	 * @return
	 */
	@Query("FROM AgentCompany  WHERE disabled = false AND companyName = :companyName")
	public AgentCompany load(@Variable("companyName") String companyName);
	
	/**
	 * 
	 * @param mobile
	 * @return
	 */
	@Query("FROM AgentCompany  WHERE disabled = false AND conactMobile = :mobile")
	public AgentCompany loadByMobile(@Variable("mobile") String mobile);
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	@Query("FROM Agent  WHERE disabled = false AND name = :name")
	public Agent loadAgent(@Variable("name") String name);
	
	/**
	 * 
	 * @param status
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM AgentApply  WHERE status = :status AND district =:district")
	public int countAll(@Variable("district") District district,@Variable("status") AgentApplyStatus status);
	
	/**
	 * 
	 * @param district
	 * @param status
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM AgentApply  WHERE status = :status AND district =:district AND disabled = false")
	public int count(@Variable("district") District district,@Variable("status") AgentApplyStatus status);
	
	/**
	 * 
	 * @param status
	 * @param id
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM AgentApply  WHERE status = :status AND principal.id =:id")
	public int countAll(@Variable("status") AgentApplyStatus status, @Variable("id") Long id);
	
	/**
	 * 
	 * @param status
	 * @param id
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM AgentApply  WHERE status = :status AND principal.id =:id AND disabled = false")
	public int count(@Variable("status") AgentApplyStatus status, @Variable("id") Long id);
	
	/**
	 * 
	 * @param status
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM AgentApply  WHERE status = :status")
	public int countAll(@Variable("status") AgentApplyStatus status);
	
	/**
	 * 
	 * @param status
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM AgentApply  WHERE status = :status AND disabled = false")
	public int count(@Variable("status") AgentApplyStatus status);
	
	/**
	 * 
	 * @param login 
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @param district 
	 * @return
	 */
	@Limit
	@Query("FROM AgentApply WHERE status = :status AND disabled = false AND district =:district")
	public List<AgentApply> list(@Variable("status") AgentApplyStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult,@Variable("district") District district);
	
	
	/**
	 * 
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @param id
	 * @return
	 */
	@Limit
	@Query("FROM AgentApply WHERE status = :status AND disabled = false AND principal.id =:id")
	public List<AgentApply> lists(@Variable("status") AgentApplyStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult,@Variable("id") Long id);
	
	/**
	 * 
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @param district
	 * @return
	 */
	@Limit
	@Query("FROM AgentApply WHERE status = :status  AND district =:district")
	public List<AgentApply> listAll(@Variable("status") AgentApplyStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult,@Variable("district") District district);
	
	/**
	 * 
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @param id
	 * @return
	 */
	@Limit
	@Query("FROM AgentApply WHERE status = :status  AND principal.id =:id")
	public List<AgentApply> listAlls(@Variable("status") AgentApplyStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult, @Variable("id") Long id);
	
	/**
	 * 根据分区查询销售
	 * @return
	 */
	@Query("FROM Principal WHERE disabled = false AND district.id =:districtId AND authority ='SALE_USER' OR authority = 'SALE_DIRECTOR' AND disabled = false AND district.id =:districtId")
	public List<Principal> list(@Variable("districtId") Long districtId);
	
	/**
	 * 
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
	@Query("FROM AgentApply WHERE status = :status AND disabled = false")
	public List<AgentApply> listAll(@Variable("status") AgentApplyStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult); 
	
	/**
	 * 
	 * @param status
	 * @param beginIndex
	 * @param maxResult
	 * @return
	 */
	@Limit
	@Query("FROM AgentApply WHERE status = :status")
	public List<AgentApply> list(@Variable("status") AgentApplyStatus status, @Variable("beginIndex") int beginIndex,@Variable("maxResult") int maxResult);
	
	/**
	 * 
	 * @return
	 */
	@Query("FROM AgentCompany WHERE disabled = false AND type =:type")
	public List<AgentCompany> listAgentCompany(@Variable("type") AgentType type);
	
	/**
	 * 
	 * @param mobile
	 * @return
	 */
	@Query("FROM AgentApply WHERE disabled = false AND mobile =:mobile")
	public List<AgentApply> loads(@Variable("mobile") String mobile);

	/**
	 * @param principal
	 * @return
	 */
	@Query("FROM AgentApply WHERE disabled = false AND principal =:principal")
    public List<AgentApply> listByPrincipal(@Variable("principal") Principal principal);

	@Query("FROM Agent WHERE disabled = false AND mobile =:mobile")
	Agent loadAgentByMobile(@Variable("mobile") String mobile);
}
